package com.rede.didiok.common.utils.file;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson.TypeReference;
import com.rede.didiok.common.constant.conf.MessageConf;
import com.rede.didiok.common.constant.conf.RedisConf;
import com.rede.didiok.common.constant.conf.SysConf;
import com.rede.didiok.common.constant.global.Constants;
import com.rede.didiok.common.constant.global.ErrorCode;
import com.rede.didiok.common.dto.admin.SystemConfigDto;
import com.rede.didiok.common.enums.OpenStatusEnum;
import com.rede.didiok.common.exception.exceptionType.FeignException;
import com.rede.didiok.common.exception.exceptionType.QueryException;
import com.rede.didiok.common.feign.AdminFeignClient;
import com.rede.didiok.common.feign.WebFeignClient;
import com.rede.didiok.common.utils.JsonUtils;
import com.rede.didiok.common.utils.R;
import com.rede.didiok.common.utils.StringUtils;

/**
 * Feign操作工具类
 *
 * @author: Rede
 * @create: 2023-02-29-15:39
 */
@Slf4j
@Component
public class FeignUtil {

    @Autowired
    AdminFeignClient adminFeignClient;

    @Autowired
    WebFeignClient webFeignClient;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 通过Token获取系统配置【返回Map类型】
     *
     * @return
     */
    public Map<String, String> getSystemConfigMap(String token) {
        // 判断该token的有效性【已在外层判断】
//        String adminJsonResult = stringRedisTemplate.opsForValue().get(RedisConf.LOGIN_TOKEN_KEY + Constants.SYMBOL_COLON + token);
//        if (StringUtils.isEmpty(adminJsonResult)) {
//            throw new QueryException(ErrorCode.INVALID_TOKEN, MessageConf.INVALID_TOKEN);
//        }
        // 从Redis中获取的SystemConf 或者 通过feign获取的
        SystemConfigDto systemConfig;
        //从Redis中获取内容
        String jsonResult = stringRedisTemplate.opsForValue().get(RedisConf.SYSTEM_CONFIG);
        // 判断Redis中是否有数据
        if (StringUtils.isNotEmpty(jsonResult)) {
            systemConfig = JsonUtils.jsonToPojo(jsonResult, SystemConfigDto.class);
        } else {
            // 通过feign获取系统配置
            R configResult = adminFeignClient.getSystemConfig();
            if(configResult.getCode()==0){
                systemConfig = configResult.getData(new TypeReference<SystemConfigDto>(){});
                //将从token存储到redis中，设置30分钟后过期
                stringRedisTemplate.opsForValue().set(RedisConf.SYSTEM_CONFIG, JsonUtils.objectToJson(systemConfig), 30, TimeUnit.MINUTES);
            }else {
                // 获取异常信息
                String msg = configResult.getData("msg",new TypeReference<String>(){});
                throw new FeignException("feign调用异常（获取系统配置）："+msg);
            }

        }
        // systemConfig 转成 map
        Map<String, String> resultMap = JsonUtils.object2Map(systemConfig);
        return resultMap;
    }

    /**
     * 获取系统配置，不论是Admin端还是Web端
     *
     * @return
     */
    public SystemConfigDto getSystemConfig() {
        SystemConfigDto systemConfig = new SystemConfigDto();
        ServletRequestAttributes attribute = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attribute.getRequest();
        // 后台携带的token
        Object token = request.getAttribute(SysConf.TOKEN);
        // 参数中携带的token
        String paramsToken = request.getParameter(SysConf.TOKEN);
        // 获取平台【web：门户，admin：管理端】
        String platform = request.getParameter(SysConf.PLATFORM);
        Map<String, String> systemConfigMap = new HashMap<>();
        // 判断是否是web端发送过来的请求【后端发送过来的token长度为32】
        if (SysConf.WEB.equals(platform)
                || (paramsToken != null && paramsToken.length() == Constants.THIRTY_TWO)
                || (token != null && token.toString().length() == Constants.THIRTY_TWO)) {
            // 如果是调用web端获取配置的接口
            if (token != null) {
                systemConfigMap = this.getSystemConfigByWebToken(token.toString());
                systemConfig.setToken(token.toString());
            } else {
                systemConfigMap = this.getSystemConfigByWebToken(paramsToken);
                systemConfig.setToken(paramsToken);
            }
            systemConfig.setPlatform(SysConf.WEB);
        } else {
            // 调用admin端获取配置接口
            if (token != null) {
                // 判断是否是后台过来的请求
                systemConfigMap = this.getSystemConfigMap(token.toString());
                systemConfig.setToken(token.toString());
            } else {
                // 判断是否是通过params参数传递过来的
                systemConfigMap = this.getSystemConfigMap(paramsToken);
                systemConfig.setToken(paramsToken);
            }
            systemConfig.setPlatform(SysConf.ADMIN);
        }

        if (systemConfigMap == null) {
            log.error(MessageConf.PLEASE_SET_QI_NIU);
            throw new QueryException(ErrorCode.PLEASE_SET_QI_NIU, MessageConf.PLEASE_SET_QI_NIU);
        }

        if (systemConfigMap == null) {
            throw new QueryException(ErrorCode.SYSTEM_CONFIG_NOT_EXIST, MessageConf.SYSTEM_CONFIG_NOT_EXIST);
        } else {
            String uploadQiNiu = systemConfigMap.get(SysConf.UPLOAD_QI_NIU);
            String uploadLocal = systemConfigMap.get(SysConf.UPLOAD_LOCAL);
            String localPictureBaseUrl = systemConfigMap.get(SysConf.LOCAL_PICTURE_BASE_URL);
            String qiNiuPictureBaseUrl = systemConfigMap.get(SysConf.QI_NIU_PICTURE_BASE_URL);
            String qiNiuAccessKey = systemConfigMap.get(SysConf.QI_NIU_ACCESS_KEY);
            String qiNiuSecretKey = systemConfigMap.get(SysConf.QI_NIU_SECRET_KEY);
            String qiNiuBucket = systemConfigMap.get(SysConf.QI_NIU_BUCKET);
            String qiNiuArea = systemConfigMap.get(SysConf.QI_NIU_AREA);

            String minioEndPoint = systemConfigMap.get(SysConf.MINIO_END_POINT);
            String minioAccessKey = systemConfigMap.get(SysConf.MINIO_ACCESS_KEY);
            String minioSecretKey = systemConfigMap.get(SysConf.MINIO_SECRET_KEY);
            String minioBucket = systemConfigMap.get(SysConf.MINIO_BUCKET);
            String uploadMinio = systemConfigMap.get(SysConf.UPLOAD_MINIO);
            String minioPictureBaseUrl = systemConfigMap.get(SysConf.MINIO_PICTURE_BASE_URL);

            String aliyunEndPoint = systemConfigMap.get(SysConf.ALI_YUN_ENDPOINT);
            String aliyunAccessKeySecret = systemConfigMap.get(SysConf.ALI_YUN_ACCESS_KEY_SECRET);
            String aliyunAccessKey = systemConfigMap.get(SysConf.ALI_YUN_ACCESS_KEY);
            String aliyunBucketName = systemConfigMap.get(SysConf.ALI_YUN_BUCKET_NAME);
            String uploadAlioss = systemConfigMap.get(SysConf.UPLOAD_ALIOSS);
            String aliossPictureBaseUrl = systemConfigMap.get(SysConf.ALI_OSS_PICTURE_BASE_URL);
            String aliyunCatalog= systemConfigMap.get(SysConf.ALI_YUN_CATALOG);
            // 判断七牛云参数是否存在异常
            if (OpenStatusEnum.OPEN.equals(uploadQiNiu) && (StringUtils.isEmpty(qiNiuPictureBaseUrl) || StringUtils.isEmpty(qiNiuAccessKey)
                    || StringUtils.isEmpty(qiNiuSecretKey) || StringUtils.isEmpty(qiNiuBucket) || StringUtils.isEmpty(qiNiuArea))) {
                throw new QueryException(ErrorCode.PLEASE_SET_QI_NIU, MessageConf.PLEASE_SET_QI_NIU);
            }
            // 判断本地服务参数是否存在异常
            if (OpenStatusEnum.OPEN.equals(uploadLocal) && StringUtils.isEmpty(localPictureBaseUrl)) {
                throw new QueryException(ErrorCode.PLEASE_SET_LOCAL, MessageConf.PLEASE_SET_LOCAL);
            }
            // 判断Minio服务是否存在异常
            if (OpenStatusEnum.OPEN.equals(uploadMinio) && (StringUtils.isEmpty(minioEndPoint) || StringUtils.isEmpty(minioPictureBaseUrl) || StringUtils.isEmpty(minioAccessKey)
                    || StringUtils.isEmpty(minioSecretKey) || StringUtils.isEmpty(minioBucket))) {
                throw new QueryException(ErrorCode.PLEASE_SET_MINIO, MessageConf.PLEASE_SET_MINIO);
            }

            //判断阿里OSS
            if (OpenStatusEnum.OPEN.equals(uploadAlioss) && (StringUtils.isEmpty(aliyunEndPoint) || StringUtils.isEmpty(aliossPictureBaseUrl) || StringUtils.isEmpty(aliyunAccessKey)
                    || StringUtils.isEmpty(aliyunAccessKeySecret) || StringUtils.isEmpty(aliyunBucketName))) {
                throw new QueryException(ErrorCode.PLEASE_SET_ALIOSS, MessageConf.PLEASE_SET_ALIOSS);
            }

            systemConfig.setQiNiuAccessKey(qiNiuAccessKey);
            systemConfig.setQiNiuSecretKey(qiNiuSecretKey);
            systemConfig.setQiNiuBucket(qiNiuBucket);
            systemConfig.setQiNiuArea(qiNiuArea);
            systemConfig.setUploadQiNiu(uploadQiNiu);
            systemConfig.setUploadLocal(uploadLocal);
            systemConfig.setPicturePriority(systemConfigMap.get(SysConf.PICTURE_PRIORITY));
            systemConfig.setContentPicturePriority(systemConfigMap.get(SysConf.CONTENT_PICTURE_PRIORITY));
            systemConfig.setLocalPictureBaseUrl(systemConfigMap.get(SysConf.LOCAL_PICTURE_BASE_URL));
            systemConfig.setQiNiuPictureBaseUrl(systemConfigMap.get(SysConf.QI_NIU_PICTURE_BASE_URL));

            systemConfig.setMinioEndPoint(minioEndPoint);
            systemConfig.setMinioAccessKey(minioAccessKey);
            systemConfig.setMinioSecretKey(minioSecretKey);
            systemConfig.setMinioBucket(minioBucket);
            systemConfig.setMinioPictureBaseUrl(minioPictureBaseUrl);
            systemConfig.setUploadMinio(uploadMinio);

            systemConfig.setAliyunEndpoint(aliyunEndPoint);
            systemConfig.setAliyunAccessKey(aliyunAccessKey);
            systemConfig.setAliyunAccessKeySecret(aliyunAccessKeySecret);
            systemConfig.setAliyunBucketName(aliyunBucketName);
            systemConfig.setAliossPictureBaseUrl(aliossPictureBaseUrl);
            systemConfig.setUploadAlioss(uploadAlioss);
            systemConfig.setAliyunCatalog(aliyunCatalog);

        }
        return systemConfig;
    }

    /**
     * 通过Token获取系统配置 【传入AdminToken】
     *
     * @param token
     * @return
     */
    public SystemConfigDto getSystemConfig(String token) {
        Map<String, String> systemConfigMap = this.getSystemConfigMap(token);
        SystemConfigDto systemConfig = new SystemConfigDto();
        if (systemConfigMap == null) {
            throw new QueryException(ErrorCode.SYSTEM_CONFIG_NOT_EXIST, MessageConf.SYSTEM_CONFIG_NOT_EXIST);
        } else {
            String uploadQiNiu = systemConfigMap.get(SysConf.UPLOAD_QI_NIU);
            String uploadLocal = systemConfigMap.get(SysConf.UPLOAD_LOCAL);
            String localPictureBaseUrl = systemConfigMap.get(SysConf.LOCAL_PICTURE_BASE_URL);
            String qiNiuPictureBaseUrl = systemConfigMap.get(SysConf.QI_NIU_PICTURE_BASE_URL);
            String qiNiuAccessKey = systemConfigMap.get(SysConf.QI_NIU_ACCESS_KEY);
            String qiNiuSecretKey = systemConfigMap.get(SysConf.QI_NIU_SECRET_KEY);
            String qiNiuBucket = systemConfigMap.get(SysConf.QI_NIU_BUCKET);
            String qiNiuArea = systemConfigMap.get(SysConf.QI_NIU_AREA);

            String minioEndPoint = systemConfigMap.get(SysConf.MINIO_END_POINT);
            String minioAccessKey = systemConfigMap.get(SysConf.MINIO_ACCESS_KEY);
            String minioSecretKey = systemConfigMap.get(SysConf.MINIO_SECRET_KEY);
            String minioBucket = systemConfigMap.get(SysConf.MINIO_BUCKET);
            String uploadMinio = systemConfigMap.get(SysConf.UPLOAD_MINIO);
            String minioPictureBaseUrl = systemConfigMap.get(SysConf.MINIO_PICTURE_BASE_URL);

            String aliyunEndPoint = systemConfigMap.get(SysConf.ALI_YUN_ENDPOINT);
            String aliyunAccessKeySecret = systemConfigMap.get(SysConf.ALI_YUN_ACCESS_KEY_SECRET);
            String aliyunAccessKey = systemConfigMap.get(SysConf.ALI_YUN_ACCESS_KEY);
            String aliyunBucketName = systemConfigMap.get(SysConf.ALI_YUN_BUCKET_NAME);
            String uploadAlioss = systemConfigMap.get(SysConf.UPLOAD_ALIOSS);
            String aliossPictureBaseUrl = systemConfigMap.get(SysConf.ALI_OSS_PICTURE_BASE_URL);
            String aliyunCatalog= systemConfigMap.get(SysConf.ALI_YUN_CATALOG);



            if (OpenStatusEnum.OPEN.equals(uploadQiNiu) && (StringUtils.isEmpty(qiNiuPictureBaseUrl) || StringUtils.isEmpty(qiNiuAccessKey)
                    || StringUtils.isEmpty(qiNiuSecretKey) || StringUtils.isEmpty(qiNiuBucket) || StringUtils.isEmpty(qiNiuArea))) {
                throw new QueryException(ErrorCode.PLEASE_SET_QI_NIU, MessageConf.PLEASE_SET_QI_NIU);
            }

            if (OpenStatusEnum.OPEN.equals(uploadLocal) && StringUtils.isEmpty(localPictureBaseUrl)) {
                throw new QueryException(ErrorCode.PLEASE_SET_LOCAL, MessageConf.PLEASE_SET_QI_NIU);
            }

            // 判断Minio服务是否存在异常
            if (OpenStatusEnum.OPEN.equals(uploadMinio) && (StringUtils.isEmpty(minioEndPoint) || StringUtils.isEmpty(minioPictureBaseUrl) || StringUtils.isEmpty(minioAccessKey)
                    || StringUtils.isEmpty(minioSecretKey) || StringUtils.isEmpty(minioBucket))) {
                throw new QueryException(ErrorCode.PLEASE_SET_MINIO, MessageConf.PLEASE_SET_MINIO);
            }
            //判断阿里OSS
            if (OpenStatusEnum.OPEN.equals(uploadAlioss) && (StringUtils.isEmpty(aliyunEndPoint) || StringUtils.isEmpty(aliossPictureBaseUrl) || StringUtils.isEmpty(aliyunAccessKey)
                    || StringUtils.isEmpty(aliyunAccessKeySecret) || StringUtils.isEmpty(aliyunBucketName))) {
                throw new QueryException(ErrorCode.PLEASE_SET_ALIOSS, MessageConf.PLEASE_SET_ALIOSS);
            }

            systemConfig.setQiNiuAccessKey(qiNiuAccessKey);
            systemConfig.setQiNiuSecretKey(qiNiuSecretKey);
            systemConfig.setQiNiuBucket(qiNiuBucket);
            systemConfig.setQiNiuArea(qiNiuArea);
            systemConfig.setUploadQiNiu(uploadQiNiu);
            systemConfig.setUploadLocal(uploadLocal);
            systemConfig.setPicturePriority(systemConfigMap.get(SysConf.PICTURE_PRIORITY));
            systemConfig.setContentPicturePriority(systemConfigMap.get(SysConf.CONTENT_PICTURE_PRIORITY));
            systemConfig.setLocalPictureBaseUrl(systemConfigMap.get(SysConf.LOCAL_PICTURE_BASE_URL));
            systemConfig.setQiNiuPictureBaseUrl(systemConfigMap.get(SysConf.QI_NIU_PICTURE_BASE_URL));

            systemConfig.setMinioEndPoint(minioEndPoint);
            systemConfig.setMinioAccessKey(minioAccessKey);
            systemConfig.setMinioSecretKey(minioSecretKey);
            systemConfig.setMinioBucket(minioBucket);
            systemConfig.setMinioPictureBaseUrl(minioPictureBaseUrl);
            systemConfig.setUploadMinio(uploadMinio);


            systemConfig.setAliyunEndpoint(aliyunEndPoint);
            systemConfig.setAliyunAccessKey(aliyunAccessKey);
            systemConfig.setAliyunAccessKeySecret(aliyunAccessKeySecret);
            systemConfig.setAliyunBucketName(aliyunBucketName);
            systemConfig.setAliossPictureBaseUrl(aliossPictureBaseUrl);
            systemConfig.setUploadAlioss(uploadAlioss);
            systemConfig.setAliyunCatalog(aliyunCatalog);


        }
        return systemConfig;
    }

    /**
     * 从Map中获取系统配置
     *
     * @param systemConfigMap
     * @return
     */
    public SystemConfigDto getSystemConfigByMap(Map<String, String> systemConfigMap) {
        SystemConfigDto systemConfig = new SystemConfigDto();
        if (systemConfigMap == null) {
            throw new QueryException(ErrorCode.SYSTEM_CONFIG_NOT_EXIST, MessageConf.SYSTEM_CONFIG_NOT_EXIST);
        } else {
            String uploadQiNiu = systemConfigMap.get(SysConf.UPLOAD_QI_NIU);
            String uploadLocal = systemConfigMap.get(SysConf.UPLOAD_LOCAL);
            String localPictureBaseUrl = systemConfigMap.get(SysConf.LOCAL_PICTURE_BASE_URL);
            String qiNiuPictureBaseUrl = systemConfigMap.get(SysConf.QI_NIU_PICTURE_BASE_URL);
            String qiNiuAccessKey = systemConfigMap.get(SysConf.QI_NIU_ACCESS_KEY);
            String qiNiuSecretKey = systemConfigMap.get(SysConf.QI_NIU_SECRET_KEY);
            String qiNiuBucket = systemConfigMap.get(SysConf.QI_NIU_BUCKET);
            String qiNiuArea = systemConfigMap.get(SysConf.QI_NIU_AREA);
            String picturePriority = systemConfigMap.get(SysConf.PICTURE_PRIORITY);
            String contentPicturePriority = systemConfigMap.get(SysConf.CONTENT_PICTURE_PRIORITY);

            String minioEndPoint = systemConfigMap.get(SysConf.MINIO_END_POINT);
            String minioAccessKey = systemConfigMap.get(SysConf.MINIO_ACCESS_KEY);
            String minioSecretKey = systemConfigMap.get(SysConf.MINIO_SECRET_KEY);
            String minioBucket = systemConfigMap.get(SysConf.MINIO_BUCKET);
            String uploadMinio = systemConfigMap.get(SysConf.UPLOAD_MINIO);
            String minioPictureBaseUrl = systemConfigMap.get(SysConf.MINIO_PICTURE_BASE_URL);

            String aliyunEndPoint = systemConfigMap.get(SysConf.ALI_YUN_ENDPOINT);
            String aliyunAccessKey = systemConfigMap.get(SysConf.ALI_YUN_ACCESS_KEY);
            String aliyunAccessKeySecret = systemConfigMap.get(SysConf.ALI_YUN_ACCESS_KEY_SECRET);
            String aliyunBucketName = systemConfigMap.get(SysConf.ALI_YUN_BUCKET_NAME);
            String uploadAlioss = systemConfigMap.get(SysConf.UPLOAD_ALIOSS);
            String aliossPictureBaseUrl = systemConfigMap.get(SysConf.ALI_OSS_PICTURE_BASE_URL);
            String aliyunCatalog= systemConfigMap.get(SysConf.ALI_YUN_CATALOG);


            if (OpenStatusEnum.OPEN.equals(uploadQiNiu) && (StringUtils.isEmpty(qiNiuPictureBaseUrl) || StringUtils.isEmpty(qiNiuAccessKey)
                    || StringUtils.isEmpty(qiNiuSecretKey) || StringUtils.isEmpty(qiNiuBucket) || StringUtils.isEmpty(qiNiuArea))) {
                throw new QueryException(ErrorCode.PLEASE_SET_QI_NIU, MessageConf.PLEASE_SET_QI_NIU);
            }

            if (OpenStatusEnum.OPEN.equals(uploadLocal) && StringUtils.isEmpty(localPictureBaseUrl)) {
                throw new QueryException(ErrorCode.PLEASE_SET_LOCAL, MessageConf.PLEASE_SET_QI_NIU);
            }

            // 判断Minio服务是否存在异常
            if (OpenStatusEnum.OPEN.equals(uploadMinio) && (StringUtils.isEmpty(minioEndPoint) || StringUtils.isEmpty(minioPictureBaseUrl) || StringUtils.isEmpty(minioAccessKey)
                    || StringUtils.isEmpty(minioSecretKey) || StringUtils.isEmpty(minioBucket))) {
                throw new QueryException(ErrorCode.PLEASE_SET_MINIO, MessageConf.PLEASE_SET_MINIO);
            }

            //判断阿里OSS
            if (OpenStatusEnum.OPEN.equals(uploadAlioss) && (StringUtils.isEmpty(aliyunEndPoint) || StringUtils.isEmpty(aliossPictureBaseUrl) || StringUtils.isEmpty(aliyunAccessKey)
                    || StringUtils.isEmpty(aliyunAccessKeySecret) || StringUtils.isEmpty(aliyunBucketName))) {
                throw new QueryException(ErrorCode.PLEASE_SET_ALIOSS, MessageConf.PLEASE_SET_ALIOSS);
            }

            systemConfig.setQiNiuAccessKey(qiNiuAccessKey);
            systemConfig.setQiNiuSecretKey(qiNiuSecretKey);
            systemConfig.setQiNiuBucket(qiNiuBucket);
            systemConfig.setQiNiuArea(qiNiuArea);
            systemConfig.setUploadQiNiu(uploadQiNiu);
            systemConfig.setUploadLocal(uploadLocal);
            systemConfig.setPicturePriority(picturePriority);
            systemConfig.setContentPicturePriority(contentPicturePriority);
            systemConfig.setContentPicturePriority(systemConfigMap.get(SysConf.CONTENT_PICTURE_PRIORITY));
            systemConfig.setLocalPictureBaseUrl(localPictureBaseUrl);
            systemConfig.setQiNiuPictureBaseUrl(qiNiuPictureBaseUrl);

            systemConfig.setMinioEndPoint(minioEndPoint);
            systemConfig.setMinioAccessKey(minioAccessKey);
            systemConfig.setMinioSecretKey(minioSecretKey);
            systemConfig.setMinioBucket(minioBucket);
            systemConfig.setMinioPictureBaseUrl(minioPictureBaseUrl);
            systemConfig.setUploadMinio(uploadMinio);


            systemConfig.setAliyunEndpoint(aliyunEndPoint);
            systemConfig.setAliyunAccessKey(aliyunAccessKey);
            systemConfig.setAliyunAccessKeySecret(aliyunAccessKeySecret);
            systemConfig.setAliyunBucketName(aliyunBucketName);
            systemConfig.setAliossPictureBaseUrl(aliossPictureBaseUrl);
            systemConfig.setUploadAlioss(uploadAlioss);
            systemConfig.setAliyunCatalog(aliyunCatalog);

        }
        return systemConfig;
    }


    /**
     * 通过Web端的token获取系统配置文件 【传入Admin端的token】
     *
     * @param token
     * @return
     */
    public Map<String, String> getSystemConfigByWebToken(String token) {
        // 判断该token的有效性
        String webUserJsonResult = stringRedisTemplate.opsForValue().get(RedisConf.USER_TOKEN + Constants.SYMBOL_COLON + token);
        if (StringUtils.isEmpty(webUserJsonResult)) {
            throw new QueryException(ErrorCode.INVALID_TOKEN, MessageConf.INVALID_TOKEN);
        }
        // 从Redis中获取的SystemConf 或者 通过feign获取的
        SystemConfigDto systemConfig;
        //从Redis中获取内容
        String jsonResult = stringRedisTemplate.opsForValue().get(RedisConf.SYSTEM_CONFIG);
        // 判断Redis中是否有数据
        if (StringUtils.isNotEmpty(jsonResult)) {
            systemConfig = JsonUtils.jsonToPojo(jsonResult, SystemConfigDto.class);
        } else {
            // 通过feign获取系统配置
            R configResult = adminFeignClient.getSystemConfig();
            if(configResult.getCode()==0){
                systemConfig = configResult.getData(new TypeReference<SystemConfigDto>(){});
                //将从token存储到redis中，设置30分钟后过期
                stringRedisTemplate.opsForValue().set(RedisConf.SYSTEM_CONFIG, JsonUtils.objectToJson(systemConfig), 30, TimeUnit.MINUTES);
            }else {
                // 获取异常信息
                String msg = configResult.getData("msg",new TypeReference<String>(){});
                throw new FeignException("feign调用异常（获取系统配置）："+msg);
            }
        }
        // systemConfig 转成 map
        Map<String, String> resultMap = JsonUtils.object2Map(systemConfig);
        return resultMap;
    }

}
